package cn.shop.cloud.common;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @program: IntelliJ IDEA
 * @ClassName: CommonControllerAdvice
 * @description:
 * @author: Song
 * @create: 2024-12-13 15 : 50
 * @version: 1.0
 **/
@ControllerAdvice
public class CommonControllerAdvice {

    //当出现BusinessException时调用此方法，以JSON格式响应到前端
    @ExceptionHandler(BusinessException.class)
    @ResponseBody
    public Result handlerBusiness(BusinessException ex){
        return Result.error(ex.getCodeMsg());
    }

    //当出现Exception时调用此方法，以JSON格式响应到前端
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Result handlerBusiness(Exception ex){
        return Result.defaultError();
    }


}
